import { modal } from 'vue-strap'
import updateInvesterBuytype from '../update-invester-buy-type/update-invester-buy-type.vue'
import { updateSubtype, getInvesterBetaList, getInvesterDetail, showMsg } from '../../../../../vuex/action.js'

export default {
  data() {
    return {
      showAddModal: false,
      betaList: [],
      subBetaIds: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 99999
      }
    }
  },
  props: ['detail'],
  computed: {
    packageParams() {
      this.detail.subBetaIds = this.subBetaIds.join(',')
      this.detail.type = this.payType
      return this.detail
    }
  },
  events: {
    on_refresh_list() {
      this.getInvesterBetalist()
    }
  },
  methods: {
    validParams(params) {
      let key
      let val
      for (key in params) {
        val = params[key]
        if (['betaId', 'name'].indexOf(key) >= 0 && (val + '').length === 0) {
          return '* 标记的均为必填/必选项'
        }
      }
      if (this.subBetaIds.length === 0) {
        return '* 标记的均为必填/必选项'
      }
    },
    initSubBetaIds() {
      let subBetaIds = this.detail.subBetaIds
      if (subBetaIds && subBetaIds.length > 0) {
        this.subBetaIds = subBetaIds.split(',').map((item) => {
          return +item
        })
      }
    },
    evtShowModal() {
      this.showAddModal = true
    },
    evtShowModalDialog() {
      this
      .getInvesterBetalist()
      .then(() => {
        this.showAddModal = true
      })
    },
    getInvesterBetalist() {
      return this.xGetInvesterBetaList({
        start: this.pages.start,
        limit: this.pages.limit,
        type: this.payType
      }).then((res) => {
        this.betaList = res.data.list
        return Promise.resolve()
      })
    },
    getPackageDetail() {
      return this.xGetInvesterDetail({
        id: this.detail.betaId,
        subType: this.detail.subType
      }).then((res) => {
        return Promise.resolve()
      })
    },
    evtUpdatePackage() {
      let validMsg = this.validParams(this.packageParams)
      if (validMsg) {
        return this.xShowMsg({
          type: 'danger',
          content: validMsg
        })
      }
      this.xUpdateSubtype(this.packageParams).then((res) => {
        this.showAddModal = false
        this.$dispatch('on_refresh_list')
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xGetInvesterBetaList: getInvesterBetaList,
      xUpdateSubtype: updateSubtype,
      xGetInvesterDetail: getInvesterDetail
    },
    getters: {
      payType: state => state.payType
    }
  },
  components: {
    'vue-modal': modal,
    'update-invester-buy-type': updateInvesterBuytype
  },
  ready() {
    this.initSubBetaIds()
  }
}
